$(function(){
	var page=1;
	var fi=1;
	
	var main=[[ //表头
		{if $Think.get.op}
		 {title: '',toolbar: '#choose',fixed: 'left',minWidth:150},
		 {else/}
		 {type:'checkbox', fixed: 'left'},
		 {/if}
        {field: 'number', title: '车牌号',  sort: true, fixed: 'left',minWidth:100, templet: '#carId'}
        ,{field: 'brand_name', title: '品牌',  sort: true,templet:`<div>{{d.brand_name?d.brand_name:"-"}}</div>`}
        ,{field: 'car_type_name', title: '车型',templet:`<div>{{d.car_type_name?d.car_type_name:"-"}}</div>`} 
        ,{field: 'company_name',width:300, title: '所属公司',templet:`<div>{{d.company_name?d.company_name:"-"}}</div>`}
        ,{field: 'register_time', width:200,title: '注册时间', sort: true,templet:`<div>{{d.register_time?d.register_time:"-"}}</div>`}
        ,{field:'state2', title:'状态',templet:`<div>{{d.state2?d.state2:"-"}}</div>`}
        {if $Think.get.op}
         {else/}
        ,{ title: '操作',minWidth:170, toolbar:'#operation',fixed: 'right'}
         {/if}
    ]];
    $('#more').on('click',function(){
    	if($('#showmore').css('display')=='none'){
    		$('#showmore').css('display','block')
    	}else{
    		$('#showmore').css('display','none')
    	}
    });
    var data={};
    data.page=page;
    data.pagenum=10;
    var where_json={}
    {if $Think.get.number}
		where_json.number=['like','%{$Think.get.number}%']
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	{if $Think.get.car_type_name}
		where_json.car_type_name=['like','%{$Think.get.car_type_name}%']
	{/if}

	{if $Think.get.company_id}
		where_json.company_id=['in','{$Think.get.company_id}']
	{/if}
	{if $Think.get.op==strength}
	var  company_strength_id= '{$Think.get.company_strength_id }';
	console.log('{$Think.get.company_strength_id }')
	if( Number(company_strength_id) == 0){
		console.log(123)
		where_json.company_strength_id=['in','{$Think.get.company_strength_id}']
	}
	{/if}
	

	{if $Think.get.is_check}
		where_json.is_check=['in','{$Think.get.is_check}']
	{/if}
	{if $Think.get.state}
		where_json["car.state"]=['in','{$Think.get.state}']
	{/if}
	
	data.where_json=JSON.stringify(where_json)
    ajaxcar(data)
    
	function ajaxcar(data){
		Load()
		ajax(api.car.list,data,'post').done(function(res) {
			Load(1)
				if(res.code==1){
					console.log(res)
					for(var i=0;i<res.list.length;i++){
						res.list[i].company_name=res.list[i].company.company_name
						res.list[i].car_type_name=res.list[i].cartype.car_type_name
						if(res.list[i].state==1){
							res.list[i].state2='启用'
						}else{
							res.list[i].state2='禁用'
						}
					}
					  var table = layui.table
					  ,form = layui.form;
					  //展示已知数据
						table.render({
						  	elem: '#car'
						    ,cellMinWidth: 80
						    ,cols: main
							,data: res.list
							,limit: data.pagenum
							,even: true
						});
						if (fi == 1) {
    				laypage.render({
    					elem: 'page',
    					count: res.count,
						layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
						theme: '#1E9FFF',
    					jump: function (obj, first) {
    						if (!first) {
    							page = obj.curr
    							data.page = page
    							pagenum = obj.limit
    							data.pagenum = pagenum
    							ajaxcar(data)
    						} else {

    						}
    					}
    				});
    				fi = 2
    			}
						{if $Think.get.op}
					table.on('tool(car)', function(obj) {
						var data1 = obj.data; //获得当前行数据
						var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
						var tr = obj.tr; //获得当前行 tr 的DOM对象
						if(layEvent === 'choose') {
							var index = parent.layer.getFrameIndex(window.name);
							parent.$('.car_id').val(data1.car_id)
							parent.$('.number').val(data1.number)
							parent.layer.close(index);
						}else if(layEvent === 'detail'){ //详情
							console.log(11)
							//do something
							window.location.href=("{:url('/html/car/car_detail')}?car_id="+data1.car_id{if $Think.get.op}+"&op={$Think.get.op}"{/if})
							//同步更新缓存对应的值
						   
						  }
					})
					{else/}
						
						table.on('tool(car)', function(obj){
							  var data1 = obj.data; //获得当前行数据
							  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
							  var tr = obj.tr; //获得当前行 tr 的DOM对象
							 
							   if(layEvent === 'del'){ //删除
							    layer.confirm('是否删除', function(index){
							    	Load();	
							    	ajax(api.car.delete,{

										car_ids:data1.car_id
									},'post').done(function(res) {
										if(res.code == 1) {	
											layer.close(index);
											Load(1)
											ajaxcar(data)
										} else {
											Load(1)
											Err(res.msg ? res.msg : '操作失败，稍候再试');
											layer.close(index);
										}
			
									}).fail(function(err) {
										Err('请求失败')
									});
							    	
							      layer.close(index);
							    });
							  } else if(layEvent === 'edit'){ //编辑
							    //do something
							   	window.location.href = " {:url('/html/car/car_add')}?car_id="+data1.car_id+"&page="+page+'&remote=1'
							    //同步更新缓存对应的值
							   
							  }  else if(layEvent === 'forbidden'){
							  	layer.confirm('是否禁用', function(index){
							    	Load()
							    	
							    	ajax(api.car.edit,{
										car_id:data1.car_id,
										company_id:data1.company_id,
										state: 9
									},'post').done(function(res) {
										if(res.code == 1) {
											Msg('禁用成功')
											ajaxcar(data)
											 layer.close(index);
											 Load(1)
											
										} else {
											 layer.close(index);
											 Load(1)
											Err(res.msg ? res.msg : '操作失败，稍候再试');
										}
									}).fail(function(err) {
										Err('请求失败')
									});
							    	
							    	
							      layer.close(index);
							    });
							  }else if(layEvent === 'startusing'){
							  	layer.confirm('是否启用', function(index){
							    	Load()
							    	
							    	ajax(api.car.edit,{
										car_id:data1.car_id,
										company_id:data1.company_id,
										state: 1
									},'post').done(function(res) {
										if(res.code == 1) {
											Msg('启用成功')
											ajaxcar(data)
											 layer.close(index);
											 Load(1)
											
										} else {
											 layer.close(index);
											 Load(1)
											Err(res.msg ? res.msg : '操作失败，稍候再试');
										}
									}).fail(function(err) {
										Err('请求失败')
									});
							    	
							      layer.close(index);
							    });
							  }else if(layEvent === 'storage'){
							  	layer.confirm('是否入库', function(index){
							    	Load()
							    	ajax(api.car.edit,{
										car_id:data1.car_id,
										company_id:data1.company_id,
										state: 1
									},'post').done(function(res) {
										if(res.code == 1) {
											Msg('车辆已入库')
											ajaxcar(data)
											 layer.close(index);
											 Load(1)
											
										} else {
											 layer.close(index);
											 Load(1)
											Err(res.msg ? res.msg : '操作失败，稍候再试');
										}
									}).fail(function(err) {
										Err('请求失败')
									});
							    	
							      layer.close(index);
							    });
							  }
						});
						{/if}
						
					//添加
					$('.add').on('click',function(){
						window.location.href='{:url("/html/car/car_add")}{if $Think.get.op}?op={$Think.get.op}{/if}'
					});
					//删除多个
					$(".delete-multiple").on('click',function(){
						var car_ids='';
						var checkStatus = table.checkStatus('car'); //test即为基础参数id对应的值
	 					console.log(checkStatus.data) //获取选中行的数据
						console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
						console.log(checkStatus.isAll ) //表格是否全选
						if(checkStatus.data.length){
						for(var dei=0;dei<checkStatus.data.length;dei++){
							if(dei==0){
								car_ids+=checkStatus.data[dei].car_id
							}else{
								car_ids+=(","+checkStatus.data[dei].car_id)
							}
						}
						layer.open({
							type: 1,
							title: false,
							id: "delete" ,
							content: '<div style="padding: 20px 100px;">是否删除</div>',
							btn: ['确定', '取消'],
							btnAlign: 'c' //按钮居中
								,
							shade: 0 //不显示遮罩
								,
							yes: function() {
								var deleteload = layer.load(1, {
									shade: [0.1, '#fff'] //0.1透明度的白色背景
								});
								
								ajax(api.car.delete, {
										car_ids:car_ids
									},'post').done(function(res) {
									console.log(res)
									layer.close(deleteload);
										if(res.code == 1) {
											layer.closeAll();
											Msg('删除成功')
											ajaxcar(data)
										} else  {
											
											Err(res.msg ? res.msg : '操作失败，稍候再试');
										}
								}).fail(function(err) {
									layer.close(deleteload);
									Err('请求失败')
								});			
							},
							btn2: function() {
								layer.closeAll();
								}
							});
						}else{
							Err('未有选择项')
						}
					});
				
				}else{
					Err(res.msg)
				}
		}).fail(function(err) {
			Err('请求失败')
		});
		
	}
	
})
